package com.cdx.spring.factory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * @author chedx
 * @data 2021/1/12 12:54 PM
 */
public class ProxyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {

        InvocationHandler handler = new InvocationHandler() {
            public Object invoke(Object o, Method method, Object[] args) throws Throwable {
                System.out.println("------- new  proxy  log-----");
                Object ret = method.invoke(bean, args);
                return ret;
            }
        };

        return Proxy.newProxyInstance(ProxyBeanPostProcessor.class.getClassLoader(),bean.getClass().getInterfaces(),handler);

    }
}
